﻿package morn.core.components
{
    import __AS3__.vec.*;
    import flash.display.*;
    import morn.core.utils.*;

    final public class AutoBitmap extends Bitmap
    {
        private var _x:Number = 0;
        private var _y:Number = 0;
        private var _width:Number;
        private var _height:Number;
        private var _sizeGrid:Array;
        private var _source:Vector.<BitmapData>;
        private var _clips:Vector.<BitmapData>;
        private var _index:int;
        private var _smoothing:Boolean;
        public var url:String;

        public function AutoBitmap()
        {
            return;
        }// end function

        override public function get x() : Number
        {
            return this._x;
        }// end function

        override public function set x(param1:Number) : void
        {
            this._x = param1;
            super.x = param1;
            return;
        }// end function

        override public function get y() : Number
        {
            return this._y;
        }// end function

        override public function set y(param1:Number) : void
        {
            this._y = param1;
            super.y = param1;
            return;
        }// end function

        override public function get width() : Number
        {
            return isNaN(this._width) ? (super.bitmapData ? (super.bitmapData.width) : (super.width)) : (this._width);
        }// end function

        override public function set width(param1:Number) : void
        {
            if (this._width != param1)
            {
                this._width = param1;
                App.render.callLater(this.changeSize);
            }
            return;
        }// end function

        override public function get height() : Number
        {
            return isNaN(this._height) ? (super.bitmapData ? (super.bitmapData.height) : (super.height)) : (this._height);
        }// end function

        override public function set height(param1:Number) : void
        {
            if (this._height != param1)
            {
                this._height = param1;
                App.render.callLater(this.changeSize);
            }
            return;
        }// end function

        public function get sizeGrid() : Array
        {
            return this._sizeGrid;
        }// end function

        public function set sizeGrid(param1:Array) : void
        {
            this._sizeGrid = param1;
            App.render.callLater(this.changeSize);
            return;
        }// end function

        override public function set bitmapData(param1:BitmapData) : void
        {
            if (param1)
            {
                this.clips = new Vector.<BitmapData>;
                this.clips.push(param1);
            }
            else
            {
                this.disposeTempBitmapdata();
                var _loc_2:String = null;
                this._clips = null;
                this._source = _loc_2;
                super.bitmapData = null;
            }
            return;
        }// end function

        public function get clips() : Vector.<BitmapData>
        {
            return this._source;
        }// end function

        public function set clips(param1:Vector.<BitmapData>) : void
        {
            this.disposeTempBitmapdata();
            this._source = param1;
            if (param1 && param1.length > 0)
            {
                super.bitmapData = param1[0];
                App.render.callLater(this.changeSize);
            }
            return;
        }// end function

        public function get index() : int
        {
            return this._index;
        }// end function

        public function set index(param1:int) : void
        {
            this._index = param1;
            if (this._clips && this._clips.length > 0)
            {
                this._index = this._index < this._clips.length && this._index > -1 ? (this._index) : (0);
                super.bitmapData = this._clips[this._index];
                super.smoothing = this._smoothing;
            }
            return;
        }// end function

        private function changeSize() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:Vector.<BitmapData> = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            if (this._source && this._source.length > 0)
            {
                _loc_1 = Math.round(this.width);
                _loc_2 = Math.round(this.height);
                this.disposeTempBitmapdata();
                _loc_3 = new Vector.<BitmapData>;
                _loc_4 = 0;
                _loc_5 = this._source.length;
                while (_loc_4 < _loc_5)
                {
                    
                    if (this._sizeGrid)
                    {
                        _loc_3.push(BitmapUtils.scale9Bmd(this._source[_loc_4], this._sizeGrid, _loc_1, _loc_2));
                    }
                    else
                    {
                        _loc_3.push(this._source[_loc_4]);
                    }
                    _loc_4++;
                }
                this._clips = _loc_3;
                this.index = this._index;
                super.width = _loc_1;
                super.height = _loc_2;
            }
            return;
        }// end function

        private function disposeTempBitmapdata() : void
        {
            var _loc_1:int = 0;
            if (this._clips)
            {
                _loc_1 = this._clips.length - 1;
                while (_loc_1 > -1)
                {
                    
                    if (this._clips[_loc_1] != this._source[_loc_1])
                    {
                        this._clips[_loc_1].dispose();
                    }
                    _loc_1 = _loc_1 - 1;
                }
                this._clips.length = 0;
            }
            return;
        }// end function

        override public function get smoothing() : Boolean
        {
            return this._smoothing;
        }// end function

        override public function set smoothing(param1:Boolean) : void
        {
            var _loc_2:* = param1;
            this._smoothing = param1;
            super.smoothing = _loc_2;
            return;
        }// end function

    }
}
